package com.wxy.zhuzhouactivity;



import com.ak.zhuzhou.R;
import com.wxy.zhuzhou.MainActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;

public class ActivityMainActivity extends FragmentActivity implements OnClickListener {
	private TextView title[] = new TextView[3];
	private TextView line;
	private int pre;
	private int lineLength;
	private Fragment[] fragment = new Fragment[3];
	private MyViewPager vp;
	boolean mIsShow = false;
	private int index;
	private int titleId[] = { R.id.activity_fragment_main_unstart,
			R.id.activity_fragment_main_start,
			R.id.activity_fragment_main_finish };

	public ActivityMainActivity() {
		fragment[0] = new ActivityFragment1();
		fragment[1] = new ActivityFragment2();
		fragment[2] = new ActivityFragment3();
	}

	private int getScreenWidth() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	void initDT() {
		for (int i = 0; i < fragment.length; i++) {
			title[i] = (TextView)findViewById(titleId[i]);
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.x_activityfragment_main);
		vp = (MyViewPager)findViewById(R.id.activity_vp);
		line = (TextView) findViewById(R.id.activity_line);
		
		ImageButton toBack = (ImageButton)findViewById(R.id.activity_frag_toback);
		toBack.setOnClickListener(this);
		initDT();
		vp.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
		vp.setOnPageChangeListener(new ChangePager());
		addMyListener();
		lineLength = getScreenWidth() / 3;
	}

	void addMyListener() {
		for (int i = 0; i < title.length; i++) {
			title[i].setOnClickListener(new OnTitleListener(i));
		}
	}

	class OnTitleListener implements OnClickListener {
		int position;

		public OnTitleListener(int position) {
			this.position = position;
		}

		@Override
		public void onClick(View v) {
			vp.setCurrentItem(position);
			for (int i = 0; i < title.length; i++) {
				title[i].setTextColor(Color.GRAY);
			}
			title[position].setTextColor(Color.parseColor("#eb4f38"));

		}

	}

	class ChangePager implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int position) {
			for (int i = 0; i < title.length; i++) {
				title[i].setTextColor(Color.GRAY);
			}
			title[position].setTextColor(Color.parseColor("#eb4f38"));
			int current = lineLength * (position);
			TranslateAnimation ta = new TranslateAnimation(pre, current, 0, 0);
			ta.setDuration(100);
			ta.setFillAfter(true);
			line.startAnimation(ta);
			pre = current;

		}

	}
	


	class MyViewPagerAdapter extends FragmentPagerAdapter {

		public MyViewPagerAdapter(FragmentManager fragmentManager) {
			super(fragmentManager);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragment[arg0];
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragment.length;
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_frag_toback:
			finish();
			break;

		default:
			break;
		}

	}

}
